package com.example.paymentsystem.strategy;

import com.example.paymentsystem.common.PaymentRequest;
import com.example.paymentsystem.common.PaymentResponse;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author YiYuan
 * @data 2025/6/25
 * @apoNote
 */
public class PaymentContext {

    private final Map<String, PaymentStrategy> strategies = new ConcurrentHashMap<>();

    public PaymentContext(Map<String, PaymentStrategy> strategyMap) {
        this.strategies.putAll(strategyMap);
    }

    public PaymentResponse executePayment(PaymentRequest request) {
        String key = request.getPaymentChannel()+ "_" + request.getPaymentType();

        PaymentStrategy strategy = strategies.get(key);

        if (strategy == null) {
            throw new IllegalArgumentException("No payment strategy found for: " + key);
        }

        return strategy.pay(request);
    }
}
